<template>
	<view class="setting-popup-content">
		<!-- 单位弹窗 -->
		<uni-popup ref="unitPopup" type="bottom" :safeArea="false" @maskClick="popupclosed">
			<my-unit-pop :unit_val="unit_val" @change="unitChange" @close="closeUnitPop"></my-unit-pop>
		</uni-popup>

		<view class="title-row">
			<text style="color: #a3aab4;" @click="close(false)">关闭</text>
			<text>默认设置</text>
			<text style="color: #3571fb;" @click="confirm">确认</text>
		</view>
		<view class="msg-row input-row">
			<view class="left">
				<iconfont :name="'type'" :size="25"></iconfont>
				<text>{{obj.name}}</text>
			</view>
			<view class="right">
				<image :src="obj.imgSrc" style="height: 150rpx;" mode="heightFix">
				</image>
			</view>
		</view>
		<view class="msg-row other-row">
			<view class="row-item">
				<view class="left">
					<uni-icons type="tune" size="25"></uni-icons>
					<text>默认单位</text>
				</view>
				<view class="right">
					<view class="unit-box" style="border: 1px solid #e5e5e5;" @click="openUnitPop">
						<text>{{_obj.unit}}</text>
						<uni-icons type="bottom" color="#b2b2b2" size="20"></uni-icons>
					</view>
				</view>
			</view>
			<view class="row-item">
				<view class="left">
					<uni-icons type="tune" size="25"></uni-icons>
					<text>库存提醒</text>
				</view>
				<view class="right">
					<my-number-box :background="'#fff'" :value="obj.repositoryAlert" :min="1" :max="999"
						@change="changeStoreThreshold" />
					<view class="unit-box">
						<text>{{_obj.unit}}</text>
					</view>
				</view>
			</view>
			<view class="row-item">
				<view class="left">
					<uni-icons type="calendar" size="25"></uni-icons>
					<text>临期提醒</text>
				</view>
				<view class="right">
					<my-number-box :background="'#fff'" :value="obj.expiredAlert" :min="1" :max="999"
						@change="changeTimeThreshold" />
					<view class="unit-box">
						<text>天</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		name: "my-fridge-obj-setting-pop",
		props: {
			obj: {}
		},
		data() {
			return {
				unit_val: [0],
			};
		},
		computed: {
			...mapState('object', ['unit_list']),
			// 解决单位不渲染的问题
			_obj() {
				return this.obj
			}
		},
		methods: {

			// 请求
			confirm() {
				uni.$api.object.updateFridgeObjSetting(this.obj)
				this.close(true)
			},

			// 事件处理
			close(flag) {
				this.$emit('close', flag)
			},
			// 单位弹窗
			unitChange(e) {
				this.obj.unit = this.unit_list[e[0]]
			},
			openUnitPop() {
				this.$refs.unitPopup.open()
				this.unit_val = [this.unit_list.indexOf(this.obj.unit)]
				this.popupopened()
			},
			closeUnitPop() {
				this.$refs.unitPopup.close()
				this.popupclosed()
			},
			// 库存阈值
			changeStoreThreshold(value) {
				this.obj.repositoryAlert = value
			},
			// 临期阈值
			changeTimeThreshold(value) {
				this.obj.expiredAlert = value
			},
			// 处理双层弹窗的遮罩层响应问题
			popupopened(e) {
				this.$emit('popupopened')
			},
			popupclosed(e) {
				this.$emit('popupclosed')
			},
		}
	}
</script>

<style lang="scss">
	.setting-popup-content {
		border-radius: 20rpx 20rpx 0 0;
		background-color: #e5e6eb;
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.title-row {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 22px;
			width: 98%;
		}

		.msg-row {
			background-color: #fff;
			border-radius: 12rpx;
			display: flex;
			justify-content: space-between;
			width: 86%;
			padding: 0 6%;
			font-size: 20px;
			align-items: center;
			margin-top: 20rpx;

			.left {
				>text {
					margin-left: 15rpx;
				}
			}
		}

		.other-row {
			display: flex;
			flex-direction: column;

			.row-item {
				width: 100%;
				display: flex;
				justify-content: space-between;
				margin: 20rpx;
				align-items: center;

				.right {
					display: flex;
					align-items: center;

					.unit-box {
						color: #333;
						padding: 5rpx;
						border-radius: 5px;
					}
				}
			}
		}
	}
</style>